package info.guerrero.cartaalta;

import java.util.ArrayList;
import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

public class PlayersActivity extends Activity {
	private ListView myList;
	private ImageView myImageView;
	private int numbCard=0;
	private String _YaMostrada[];
	private int cuandocarta=1;
	private AdapterListPlayers myAdapterListPlayers;
	private static int i=0;
	private String allCards[] ={"o1","o2","o3","o4","o5","o6","o7","o8","o9","o10",
    		"b1","b2","b3","b4","b5","b6","b7","b8","b9","b10",
    		"e1","e2","e3","e4","e5","e6","e7","e8","e9","e10",
    		"c1","c2","c3","c4","c5","c6","c7","c8","c9","c10"
    	};
	
	private int getCard(){
		Bitmap myCard;
		String resul;
        Random aleatorio = new Random();
        if( cuandocarta == 40){
            return 0;
        }
        //imagen=(ImageView) this.findViewById(R.id.imageViewCard);
        
        
        numbCard = aleatorio.nextInt(39);
        
        resul = allCards[numbCard];
        int a=0;
        while(_YaMostrada[a]==resul){
        	numbCard = aleatorio.nextInt(39);
        	resul = allCards[numbCard];
        	Log.d("Pene"+i, "como crece: )"+i+"cm");
        	i++;
        	a++;
        };
        _YaMostrada[cuandocarta - 1] = allCards[numbCard];
        
        int pene = getResources().getIdentifier(allCards[numbCard], "drawable", "info.guerrero.cartaalta");
		//myCard=(BitmapFactory.decodeResource(getResources(),getResources().getIdentifier(allCards[numbCard],"drawable", "info.guerrero.cartaalta")));
		
		//imagen.setImageBitmap(myCard);
		
		_YaMostrada[cuandocarta - 1] = resul;
		cuandocarta++;
        return pene;
}
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_players);
		myList = (ListView) this.findViewById(R.id.listViewCartas);
		//imagen = (ImageView) this.findViewById(R.id.imageView1);

		    ArrayList<Player> playersItems= generatePlayers();
		    myAdapterListPlayers = new AdapterListPlayers(this,playersItems);
		  		         
		    myList.setAdapter(myAdapterListPlayers); 
		    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			    @Override
			    public void onItemClick(AdapterView adapter, View view, int position, long arg){
			 
			    }
			});
		  }
	public void barajar(){
		
	}
	private ArrayList<Player> generatePlayers() {	
	  ArrayList<Player> playerList= new ArrayList<Player>();
	  myAdapterListPlayers = new AdapterListPlayers(this,playerList);
	  	for(int a=0; a<Publico.numberOfPlayers;a++){
	  		int cardplayer=getCard();
	  		if(cardplayer == 0){
	  			Log.d("error al cargar", "vale null el puto get card");			
	  		}
	  		//String sd = R.id.imageViewCard
	  		myImageView = (ImageView) this.findViewById(R.id.imageViewCard);
	  		myImageView.setImageResource(cardplayer);		
	  		Player lilplayer = new Player(a,myImageView);
  			playerList.add(lilplayer);
	  	}
		  	 	
  	return playerList;
  	
	}
		  
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.players, menu);
		return true;
	}


}
